<?php
/**
 * 文件列表
 * User: DXN
 * Date: 2020/08/05
 */

namespace App\Service\Core\Admin\File;

use App\Component\ListService\ListAbstract;
use App\Model\Core\Admin\AdminFileModel;

class AdminFileListService extends ListAbstract
{

    //允许获取的字段
    protected array $allow_fields = [];
    //允许获取的扩展字段
    protected array $allow_ext_fields = ['file_type_name', 'operate_type_name', 'status_name'];
    //默认返回的字段
    protected array $default_fields = [];
    protected array $allow_sort = ['file_id', 'created_at'];
    protected array $default_sort_by = ['file_id' => 'desc'];


    private object $AdminFileModel;

    public function __construct()
    {
        $this->AdminFileModel = AdminFileModel::getInstance();
    }

    /**
     * 文件类型
     * @param int $file_type
     * @return $this
     */
    public function setFileType($file_type)
    {
        $file_type = intval($file_type);
        $this->setParam('file_type', $file_type);
        return $this;
    }

    /**
     * 操作类型
     * @param int $operate_type
     * @return $this
     */
    public function setOperateType($operate_type)
    {
        $operate_type = intval($operate_type);
        $this->setParam('operate_type', $operate_type);
        return $this;
    }

    /**
     * 状态
     * @param int $status
     * @return $this
     */
    public function setStatus($status)
    {
        $status = intval($status);
        $this->setParam('status', $status);
        return $this;
    }

    /**
     * 备注
     * @param string $remark
     * @return $this
     */
    public function setRemark($remark)
    {
        $remark = trim($remark);
        $this->setParam('remark', $remark);
        return $this;
    }

    /**
     * 创建时间
     * @param string $begin_datetime
     * @param string $end_datetime
     * @return $this
     */
    public function setCreatedAt($begin_datetime, $end_datetime)
    {
        $this->setParam('created_at_begin', $begin_datetime);
        $this->setParam('created_at_end', $end_datetime);
        return $this;
    }

    protected function getQueryObj()
    {

        $query_obj = $this->AdminFileModel->getConnectionTable();

        //备注
        $remark = $this->getParam('remark');
        if (!empty($remark)) {
            $query_obj->where('remark', 'like', "%$remark%");
        }

        // 文件类型
        $file_type = $this->getParam('file_type');
        if ($file_type > 0) {
            $query_obj->where('file_type', $file_type);
        }

        // 操作类型
        $operate_type = $this->getParam('operate_type');
        if ($operate_type > 0) {
            $query_obj->where('operate_type', $operate_type);
        }

        // 状态
        $status = $this->getParam('status');
        if ($status > 0) {
            $query_obj->where('status', $status);
        }

        //开始时间
        $created_at_begin = $this->getParam('created_at_begin');
        if (!empty($created_at_begin)) {
            $created_at_begin = date('Y-m-d H:i:s', strtotime($created_at_begin));
            $query_obj->where('created_at', '>=', $created_at_begin);
        }
        //结束时间
        $created_at_end = $this->getParam('created_at_end');
        if (!empty($created_at_end)) {
            $created_at_end = date('Y-m-d H:i:s', strtotime($created_at_end));
            $query_obj->where('created_at', '<=', $created_at_end);
        }

        return $query_obj;
    }

    /**
     * 获取扩展字段
     * @param array $row
     * @return array
     */
    protected function getExtField(array $row)
    {
        if (easy_in_array('file_type_name', $this->ext_fields)) {
            $row['file_type_name'] = AdminFileModel::$export_type[$row['file_type']];
        }
        if (easy_in_array('operate_type_name', $this->ext_fields)) {
            $row['operate_type_name'] = AdminFileModel::$operate_type[$row['operate_type']];
        }
        if (easy_in_array('status_name', $this->ext_fields)) {
            $row['status_name'] = AdminFileModel::$status[$row['status']];
        }
        return $row;
    }
}
